package com.dorm.service;

import com.dorm.entity.DisciplinaryRecord;
import com.dorm.mapper.DisciplinaryMapper;
import com.dorm.util.Debug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.ArrayList;

@Service
public class DisciplinaryService {
    private final DisciplinaryMapper disciplinaryMapper;

    @Autowired
    public DisciplinaryService(DisciplinaryMapper disciplinaryMapper) {
        this.disciplinaryMapper = disciplinaryMapper;
    }

    public List<DisciplinaryRecord> getAllRecords() {
        try {
            Debug.log("开始查询所有违纪记录");
            List<DisciplinaryRecord> records = disciplinaryMapper.findAll();
            if (records == null) {
                Debug.log("未找到违纪记录，返回空列表");
                return new ArrayList<>();
            }
            Debug.log("成功查询到违纪记录，数量: " + records.size());
            return records;
        } catch (Exception e) {
            Debug.error("查询违纪记录失败:", e);
            throw new RuntimeException("查询违纪记录失败: " + e.getMessage());
        }
    }

    public List<DisciplinaryRecord> getStudentRecords(Long studentId) {
        try {
            return disciplinaryMapper.findByStudentId(studentId);
        } catch (Exception e) {
            Debug.error("获取学生违纪记录失败:", e);
            throw new RuntimeException("获取学生违纪记录失败: " + e.getMessage());
        }
    }

    @Transactional
    public DisciplinaryRecord createRecord(DisciplinaryRecord record) {
        try {
            disciplinaryMapper.insert(record);
            return record;
        } catch (Exception e) {
            Debug.error("创建违纪记录失败:", e);
            throw new RuntimeException("创建违纪记录失败: " + e.getMessage());
        }
    }

    @Transactional
    public void deleteRecord(Long id) {
        try {
            disciplinaryMapper.deleteById(id);
        } catch (Exception e) {
            Debug.error("删除违纪记录失败:", e);
            throw new RuntimeException("删除违纪记录失败: " + e.getMessage());
        }
    }

    public List<DisciplinaryRecord> getByBuilding(Long buildingId) {
        try {
            Debug.log("获取宿舍楼违纪记录, buildingId: " + buildingId);
            List<DisciplinaryRecord> records = disciplinaryMapper.findByBuildingId(buildingId);
            Debug.log("获取到违纪记录数量: " + records.size());
            return records;
        } catch (Exception e) {
            Debug.error("获取宿舍楼违纪记录失败:", e);
            throw new RuntimeException("获取宿舍楼违纪记录失败: " + e.getMessage());
        }
    }
} 